//
//  HMVisitorView.swift
//  HMWeibo12
//
//  Created by 陈伯共 on 16/9/17.
//  Copyright © 2016年 陈伯共. All rights reserved.
//

import UIKit

// 1.定义协议
@objc protocol HMVisitorViewDelegate: NSObjectProtocol{
    // 2.定义协议方法
   @objc optional func visitorViewDidClickRegister() -> Void
   @objc optional func visitorViewDidClickLogin() -> Void
    
}

/// 访客视图，登录界面
class HMVisitorView: UIView {

    // 3.定义代理属性
    weak var delegate: HMVisitorViewDelegate?
    
    
    // OC initWithCoder:, 使用storyboard 或 xib 的时候会调用
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        // 干一些事情
        // 设置背景颜色
        self.backgroundColor = UIColor(white: 237 / 255.0, alpha: 1)
        self.setupUI()
    }
    private func setupUI() {
        
        // 添加子控件
        self.addSubview(iconView)
        self.addSubview(coverView)
        self.addSubview(houseView)
        self.addSubview(messageLabel)
        self.addSubview(registerButton)
        self.addSubview(loginButton)
        
        // 添加约束，最好是在父控件有view的时候添加约束
        // 取消Autoresizing对autoLayout的影响
        iconView.translatesAutoresizingMaskIntoConstraints = false
        coverView.translatesAutoresizingMaskIntoConstraints = false
        houseView.translatesAutoresizingMaskIntoConstraints = false
        messageLabel.translatesAutoresizingMaskIntoConstraints = false
        registerButton.translatesAutoresizingMaskIntoConstraints = false
        loginButton.translatesAutoresizingMaskIntoConstraints = false
        
        /*
         item:要约束的view
         attribute:要约束的view的属性
         relatedBy:参照关系
         toItem:要参照的view
         attribute:要参照的属性
         multiplier:乘积 默认1
         constant:距离
         */
        // 约束需要添加到view上面才能起作用，约束添加到要约束的view的父控件
        // 转轮
        // centerX
        self.addConstraint(NSLayoutConstraint(item: iconView, attribute: .centerX, relatedBy: .equal, toItem: self, attribute: .centerX, multiplier: 1, constant: 0))
        // centerY
        self.addConstraint(NSLayoutConstraint(item: iconView, attribute: .centerY, relatedBy: .equal, toItem: self, attribute: .centerY, multiplier: 1, constant: -40))
        
        
        // 遮盖
        // centerX
        self.addConstraint(NSLayoutConstraint(item: coverView, attribute: .centerX, relatedBy: .equal, toItem: iconView, attribute: .centerX, multiplier: 1, constant: 0))
        // centerY
        self.addConstraint(NSLayoutConstraint(item: coverView, attribute: .centerY, relatedBy: .equal, toItem: iconView, attribute: .centerY, multiplier: 1, constant: 0))
        
        // 房子
        // centerX
        self.addConstraint(NSLayoutConstraint(item: houseView, attribute: .centerX, relatedBy: .equal, toItem: iconView, attribute: .centerX, multiplier: 1, constant: 0))
        // centerY
        self.addConstraint(NSLayoutConstraint(item: houseView, attribute: .centerY, relatedBy: .equal, toItem: iconView, attribute: .centerY, multiplier: 1, constant: 0))
        
        // 消息label
        // centerX
        self.addConstraint(NSLayoutConstraint(item: messageLabel, attribute: .centerX, relatedBy: .equal, toItem: self, attribute: .centerX, multiplier: 1, constant: 0))
        // top = self.centeryY + 间距
        self.addConstraint(NSLayoutConstraint(item: messageLabel, attribute: .top, relatedBy: .equal, toItem: iconView, attribute: .bottom, multiplier: 1, constant: 16))
        // width, 当不参照任何view时 attribute必须写.notAnAttribute
        self.addConstraint(NSLayoutConstraint(item: messageLabel, attribute: .width, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1, constant: 250))
        
        // 注册按钮
        // 左对齐, leading,书写书序的起始位置
        self.addConstraint(NSLayoutConstraint(item: registerButton, attribute: .leading, relatedBy: .equal, toItem: messageLabel, attribute: .leading, multiplier: 1, constant: 0))
        // top = message.bottom + 16
        self.addConstraint(NSLayoutConstraint(item: registerButton, attribute: .top, relatedBy: .equal, toItem: messageLabel, attribute: .bottom, multiplier: 1, constant: 16))
        // width
        self.addConstraint(NSLayoutConstraint(item: registerButton, attribute: .width, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1, constant: 100))
        
        // height
        self.addConstraint(NSLayoutConstraint(item: registerButton, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1, constant: 35))
        
        // 登录按钮
        // 右对齐, leading,书写书序的起始位置, trailing书写书序的结束位置
        self.addConstraint(NSLayoutConstraint(item: loginButton, attribute: .trailing, relatedBy: .equal, toItem: messageLabel, attribute: .trailing, multiplier: 1, constant: 0))
        // top = message.bottom + 16
        self.addConstraint(NSLayoutConstraint(item: loginButton, attribute: .top, relatedBy: .equal, toItem: registerButton, attribute: .top, multiplier: 1, constant: 0))
        // width
        self.addConstraint(NSLayoutConstraint(item: loginButton, attribute: .width, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1, constant: 100))
        
        // height
        self.addConstraint(NSLayoutConstraint(item: loginButton, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1, constant: 35))
        
    }
    
    // 转轮的旋转动画
    func rotationAnimation() -> Void {
        // 使用核心动画
        let anim = CABasicAnimation(keyPath: "transform.rotation")
        // 设置参数
        anim.duration = 20
        anim.toValue = 2 * M_PI
        // 不停地转
        anim.repeatCount = MAXFLOAT
        // 动画完成时不要去移除
        anim.isRemovedOnCompletion = false
        // 核心动画添加到iconView的图层上
        iconView.layer.add(anim, forKey: nil)
    }
    
    
    /// 设置访客视图的内容
    func setupVisitorInfo(imageName: String,message: String ) -> Void {
        // 修改转轮的图片
        iconView.image = UIImage(named: imageName)
        
        // 修改文字
        messageLabel.text = message
        
        // 隐藏小房子
        houseView.isHidden = true
        
        // 将遮盖放在最底层
        self.sendSubview(toBack: coverView)
        
    }
    
    
    // MARk - 按钮点击事件
    func registerDidClick() -> Void {
//        print("registerDidClick")
        // 4.调用代理方法
        delegate?.visitorViewDidClickRegister?()
    }
    
    func loginDidClick() -> Void {
//        print("loginDidClick")
        // 4.调用代理方法
        delegate?.visitorViewDidClickLogin?()
    }
    
    // MARK: - 懒加载
    /// 转轮
    private lazy var iconView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    
    // 遮盖
    private lazy var coverView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
    
    /// 小房子
    private lazy var houseView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    
    /// 消息label
    private lazy var messageLabel: UILabel = {
        let label = UILabel()
        
        // 设置label
        label.font = UIFont.systemFont(ofSize: 16)
        label.textColor = UIColor.darkGray
        label.numberOfLines = 0
        label.textAlignment = NSTextAlignment.center
        label.text = "关注一些人,看看有什么惊喜!"
        
        return label
    }()
    
    /// 注册按钮
    private lazy var registerButton: UIButton = {
        let button = UIButton()
        
        // 设置按钮
        button.setBackgroundImage(UIImage(named:"common_button_white_disable"), for: .normal)
        button.setTitle("注册", for: .normal)
        button.setTitleColor(UIColor.orange, for: .normal)
        
        // 注册按钮点击事件
        button.addTarget(self, action: #selector(registerDidClick), for: .touchUpInside)
        
        return button
    }()
    
    /// 登录按钮
    private lazy var loginButton: UIButton = {
        let button = UIButton()
        
        // 设置按钮
        button.setBackgroundImage(UIImage(named:"common_button_white_disable"), for: .normal)
        button.setTitle("登录", for: .normal)
        button.setTitleColor(UIColor.darkGray, for: .normal)
        
        // 登录按钮点击事件
        button.addTarget(self, action: #selector(loginDidClick), for: .touchUpInside)
        
        return button
    }()
}
